#include "STC8G.H"
#include "timer.h"
#include "type.h"

#define MAX_TIMER_NUM 2

static timer_handle_t xdata timer_handles[MAX_TIMER_NUM] = {0}; // initialize is necessary!!!

uint16_t loadInitTime(uint16_t us) //@11.0592MHz 16bit timer
{
#if USE_12T_MODE
    return 0xffff - us * 11.0592 / 12;
#else
    return 0xffff - us * 11.0592 / 1;
#endif
}

timer_handle_t *timerInit(uint16_t us, callback_func_t func, void *user_param)
{
    timer_handle_t *local_handle; // 声明局部变量
    uint8_t i = 0;

    while (timer_handles[i++].is_running == true && i < MAX_TIMER_NUM)
        ;
    i--;
    if (timer_handles[i].is_running)
    {
        return NULL;
    }

    timer_handles[i].time = loadInitTime(us);
    timer_handles[i].interrupt_cb.func = func;
    timer_handles[i].interrupt_cb.user_param = user_param;

    local_handle = &timer_handles[i];

    switch (i)
    {
    case TIMER0:

#if USE_12T_MODE
        AUXR &= 0x7F; // 定时器时钟12T模式
        TMOD &= 0xF0; // 设置定时器模式
#else
        AUXR |= 0x80; // 定时器0工作在1T模式
        TMOD &= 0xF0; // 清除定时器0模式位
        TMOD |= 0x01; // 设置定时器0为模式1（16位自动重装载）
#endif

        ET0 = 1; // 使能定时器0中断

        TL0 = local_handle->time & 0xFF;        // 设置定时初值低8位
        TH0 = (local_handle->time >> 8) & 0xFF; // 设置定时初值高8位

        TF0 = 0; // 清除TF1标志
        TR0 = 1; // 启动定时器0
        break;
    case TIMER1:

#if USE_12T_MODE
        AUXR |= 0x40; // 定时器时钟1T模式
        TMOD &= 0x0F; // 设置定时器模式
#else
        AUXR |= 0x40; // 定时器时钟1T模式
        TMOD &= 0x0F; // 设置定时器模式
#endif

        ET1 = 1;

        TL1 = local_handle->time & 0xFF;        // 设置定时初始值
        TH1 = (local_handle->time >> 8) & 0xFF; // 设置定时初始值

        TF1 = 0; // 清除TF1标志
        TR1 = 1; // 定时器1开始计时
        break;
        // case TIMER2:
        //     AUXR |= 0x04; // 定时器时钟1T模式
        //     T2L = local_handle->time & 0xFF;
        //     T2H = (local_handle->time >> 8) & 0xFF;
        //     AUXR |= 0x10; // 定时器2开始计时

        // #define ET2 0x04s
        //         IE2 |= ET2; // 使能定时器中断
        //         break;
        // case TIMER3:
        //     T4T3M |= 0x02; // 定时器时钟1T模式
        //     T3L = local_handle->time & 0xFF;
        //     T3H = (local_handle->time >> 8) & 0xFF;
        //     T4T3M |= 0x08; // 定时器3开始计时
        //     break;
        // case TIMER4:
        //     T4T3M |= 0x20; // 定时器时钟1T模式
        //     T4L = local_handle->time & 0xFF;
        //     T4H = (local_handle->time >> 8) & 0xFF;
        //     T4T3M |= 0x80; // 定时器4开始计时
        //     break;
    default:
        return NULL;
    }
    EA = 1; // Open the total interrupt
    local_handle->is_running = true;

    return local_handle;
}

void timerStop(timer_handle_t *handle)
{
    uint8_t timer_id = 0;

    while (handle != &timer_handles[timer_id++] && timer_id < MAX_TIMER_NUM)
        ;
    timer_id--;
    if (timer_handles[timer_id].is_running == false)
        return;

    handle->is_running = false;

    switch (timer_id)
    {
    case TIMER0:
        TR0 = 0; // Stop timer0
        break;
    case TIMER1:
        TR1 = 0; // Stop timer1
        break;
    // case TIMER2:
    //     break;
    default:
        return;
    }
}
//////////////////////////ISR/////////////////////////////////////
//////////////////////////ISR/////////////////////////////////////
//////////////////////////ISR/////////////////////////////////////

void Timer0_ISR() interrupt 1
{
    timer_handle_t *local_handle;
    local_handle = &timer_handles[0];
    if (local_handle && local_handle->is_running && local_handle->interrupt_cb.func)
    {
        TH0 = (local_handle->time >> 8) & 0xFF; // Set the timer high 8 bits
        TL0 = local_handle->time & 0xFF;        // Set the timer low 8 bits
        local_handle->interrupt_cb.func(local_handle->interrupt_cb.user_param);
    }
}
void Timer1_ISR() interrupt 3
{
    timer_handle_t *local_handle;
    local_handle = &timer_handles[1];
    if (local_handle && local_handle->is_running && local_handle->interrupt_cb.func)
    {
        TH1 = (local_handle->time >> 8) & 0xFF; // Set the timer high 8 bits
        TL1 = local_handle->time & 0xFF;        // Set the timer low 8 bits
        local_handle->interrupt_cb.func(local_handle->interrupt_cb.user_param);
    }
}
// void Timer2_ISR() interrupt 12
// {
//     timer_handle_t *local_handle;

// #define T2IF 0x01
//     AUXINTIF &= ~T2IF; // clear interrupt flag

//     local_handle = &timer_handles[2];
//     if (local_handle && local_handle->is_running && local_handle->interrupt_cb.func)
//     {
//         T2H = (local_handle->time >> 8) & 0xFF; // Set the timer high 8 bits
//         T2L = local_handle->time & 0xFF;        // Set the timer low 8 bits
//         local_handle->interrupt_cb.func(local_handle->interrupt_cb.user_param);
//     }
// }
// void Timer3_ISR() interrupt 7
// {
//     timer_handle_t *local_handle;
//     local_handle = &timer_handles[3];
//     if (local_handle && local_handle->is_running && local_handle->interrupt_cb.func)
//     {
//         T3H = (local_handle->time >> 8) & 0xFF; // Set the timer high 8 bits
//         T3L = local_handle->time & 0xFF;        // Set the timer low 8 bits
//         local_handle->interrupt_cb.func(local_handle->interrupt_cb.user_param);
//     }
// }
// void Timer4_ISR() interrupt 15
// {
//     timer_handle_t *local_handle;
//     local_handle = &timer_handles[4];
//     if (local_handle && local_handle->is_running && local_handle->interrupt_cb.func)
//     {
//         T4H = (local_handle->time >> 8) & 0xFF; // Set the timer high 8 bits
//         T4L = local_handle->time & 0xFF;        // Set the timer low 8 bits
//         local_handle->interrupt_cb.func(local_handle->interrupt_cb.user_param);
//     }
// }
